define(function(require,exports,module){
    var Grid = require("grid");

    module.exports = {
        initComponent: function(widget){
            var grid = new Grid({
                parentNode: ".J-subGrid",
                model: {
                	head: {
                		buttons: [{
                			id: "add",
                			icon: "icon-plus",
                			handler: function(){
                				widget.handler.subGrid.handleAddBtnClick(widget);
                			}
                		}]
                	},
                    columns : [{
                    	name: "productType.name",
                    	label: "产品类型"
                    },{
                    	name: "productBatch.name",
                    	label: "产品批次"
                    },{
                    	name: "price",
                    	label: "产品价格（元）",
                    	format: function(value,row){
                    		return Number(value).toFixed(2);
                    	}
                    },{
                    	name: "validity",
                    	label: "有效期（年）"
                    },{
                    	name: "liveDays",
                    	label: "居住期限"
                    },{
                    	name: "unit.value",
                    	label: "期限单位"
                    },{
                    	name: "settlementsRules",
                    	label: "费用抵扣规则",
                    	format: function(value,row){
                    		var ret = "";
                    		for(var i=0;i<value.length;i++){
                    			if(value[i].type.key == "Expense"){
                    				ret += "<div>"+value[i].name+"</div>";
                    			}
                    		}
                    		return ret;
                    	}
                    },{
                    	name: "ruleScopes",
                    	label: "费用抵扣适用范围",
                    	format: function(value,row){
                    		var ret = "";
                    		for(var i=0;i<value.length;i++){
                    			if(value[i].ruleType.key == "Expense"){
                    				ret += "<div>"+value[i].name+"</div>";
                    			}
                    		}
                    		return ret;
                    	}
                    },{
                    	name: "settlementsRules",
                    	label: "床位费折扣",
                    	format: function(value,row){
                    		var ret = "";
                    		for(var i=0;i<value.length;i++){
                    			if(value[i].type.key == "BedSubsidy"){
                    				ret += "<div>"+value[i].name+"</div>";
                    			}
                    		}
                    		return ret;
                    	}
                    },{
                    	name: "ruleScopes",
                    	label: "床位费折扣适用范围",
                    	format: function(value,row){
                    		var ret = "";
                    		for(var i=0;i<value.length;i++){
                    			if(value[i].ruleType.key == "BedSubsidy"){
                    				ret += "<div>"+value[i].name+"</div>";
                    			}
                    		}
                    		return ret;
                    	}
                    },{
                    	name: "settlementsRules",
                    	label: "返现规则",
                    	format: function(value,row){
                    		var ret = "";
                    		for(var i=0;i<value.length;i++){
                    			if(value[i].type.key == "Coupon"){
                    				ret += "<div>"+value[i].name+"</div>";
                    			}
                    		}
                    		return ret;
                    	}
                    },{
                    	name: "ruleScopes",
                    	label: "返现适用范围",
                    	format: function(value,row){
                    		var ret = "";
                    		for(var i=0;i<value.length;i++){
                    			if(value[i].ruleType.key == "Coupon"){
                    				ret += "<div>"+value[i].name+"</div>";
                    			}
                    		}
                    		return ret;
                    	}
                    },{
                    	name: "seal",
                    	label: "状态",
                    	format: function(value){
                    		return value ? "停售" : "在售";
                    	}
                    },{
                    	name: "operate",
                    	label: "操作",
                    	format: "button",
                    	formatparams: [{
                    		id: "onSale",
                    		text: "在售",
                    		show: function(value,row){
                    			return row.seal;
                    		},
                    		handler: function(index,data,rowEL){
                    			widget.handler.subGrid.handleChgBtnClick(widget,index,data);
                    		}
                    	},{
                    		id: "offSale",
                    		text: "停售",
                    		show: function(value,row){
                    			return !row.seal;
                    		},
                    		handler: function(index,data,rowEL){
                    			widget.handler.subGrid.handleChgBtnClick(widget,index,data);
                    		}
                    	},{
                    		id: "edit",
                    		icon: "icon-edit",
                    		show: function(value,row){
                    			return row.seal;
                    		},
                    		handler: function(index,data,rowEL){
                    			console.log(data);
                    			widget.handler.subGrid.handleEditBtnClick(widget,index,data);
                    		}
                    	},{
                    		id: "remove",
                    		icon: "icon-remove",
                    		show: function(value,row){
                    			return row.seal;
                    		},
                    		handler: function(index,data,rowEL){
                    			widget.handler.subGrid.handleDelBtnClick(widget,index,data);
                    		}
                    	}]
                    }]
                }
            });
            widget.set("subGrid",grid);
        }
    };
});